package com.zxy.supplier_system.server.entity

import jakarta.persistence.*
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.OffsetDateTime

/**
 * 采购入库
 *
 * 采购入库单是实际收货入库的记录
 */
@Entity
@EntityListeners(AuditingEntityListener::class)
@DynamicInsert
@DynamicUpdate
class ProcurementReceipt {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id = 0

    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var createdBy: Employee

    @CreatedDate
    lateinit var createdDateTime: OffsetDateTime

    var validatedDateTime: OffsetDateTime? = null

    @OneToOne
    @JoinColumn
    var procurement:Procurement? = null

    var sequence: Int = 0

    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var supplier : ProcurementSupplier

    @OneToMany(mappedBy = "procurementReceipt",cascade = [CascadeType.ALL],orphanRemoval = true)
    var items: MutableList<ProcurementReceiptItem> = mutableListOf()

    var note: String? = null

}

val List<ProcurementReceiptItem>.nextSequence: Int
    get() = this.maxOfOrNull {
        it.sequence
    }?.plus(1)?:0